package de.szut.ita13.rs.wave.entites.trail;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import de.szut.ita13.rs.wave.controller.Controller;
import de.szut.ita13.rs.wave.entity.Entity;
import de.szut.ita13.rs.wave.entity.Type;

public class Trail extends Entity {
	
	private float life;
	
	private float alpha = 1;
	private Controller controller;

	public Trail(int x, int y, Type type, Color color, int width, int height, float life, Controller controller) {
		super(x, y, type);
		this.color = color;
		this.life = life;
		this.width = width;
		this.height = height;
		this.controller = controller;
	}

	public void tick() {
		if(alpha > life) {
			alpha -= (life - 0.001f);
		} else {
			controller.modificateEntity(this, Controller.Modification.REMOVE);
		}
	}

	public void render(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setComposite(makeTransparent(alpha));
		g.setColor(color);
		g.fillRect(x, y, width, height);
		g2d.setComposite(makeTransparent(1));
	}
	
	private AlphaComposite makeTransparent(float alpha) {
		int type = AlphaComposite.SRC_OVER;
		return AlphaComposite.getInstance(type, alpha);
	}

	public Rectangle getBounds() {
		return new Rectangle(x, y, width, height);
	}

	
	
	
}
